<?php
/**
 * Enter description here ...
 * @author Micou13630
 *
 */
class JSONConnector extends DSConnector {

	private $DA;
	
	private $requestDao;
	
	/**
	 * Constructeur
	 */
	public function JSONConnector(IDataAccessDao $da, HttpRequest $req) {
		$this->DA = $da;
		$this->requestDao = new RequestDao($req);	
	}
	
	/**
	 * @see DSConnector::fetch()
	 */
	protected function fetch() {
		return $this->DA->fetch($this->requestDao);
	}
	
	/**
	 * @see DSConnector::add()
	 */
	protected function add() {
		return $this->DA->add($this->requestDao);
	}
	
	/**
	 * @see DSConnector::update()
	 */
	protected function update() {
		return $this->DA->update($this->requestDao);
	}
	
	/**
	 * @see DSConnector::remove()
	 */
	protected function remove() {
		return $this->DA->remove($this->requestDao);
	}
	
	/**
	 * Renvoie le requestDao
	 */
	protected function getRequestDao() {
		return $this->requestDao;
	}
	
	/**
	 * Enter description here ...
	 * @param unknown_type $any
	 */
	protected function formatResponse(IDataSetDao $dataSet) {
		$reply = array ('response' => array());
		$reply['response']['status'] = $dataSet->getStatus();
		
		$sr = $dataSet->getStartRow();
		if (isset($sr)) $reply['response']['startRow'] 	= $sr;
		$er = $dataSet->getEndRow();
		if (isset($er)) $reply['response']['endRow'] 	= $er;
		$tr = $dataSet->getTotalRows();
		if (isset($tr)) $reply['response']['totalRows'] = $tr;
		
		if ($dataSet->getStatus() === 0) $reply['response']['data'] = $dataSet->getDatas();
		else $reply['response']['errors'] = $dataSet->getErrors();
		
		return json_encode($reply);
	}
}
?>
